package com.woniu.smallmedical.controller;

import com.woniu.smallmedical.entity.ResponseResult;
import com.woniu.smallmedical.entity.Role;
import com.woniu.smallmedical.server.RoleServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class RoleController {
    @Autowired
    RoleServer roleServer;
    // 获取所有角色
    @GetMapping("/getAllRole")
    public ResponseResult getAllRole(){
        System.out.println("角色！！！！");
        List<Role>  allRole= roleServer.getAllRole();
        return new ResponseResult(allRole,200,"角色！");
    }
//    新增角色
    @PostMapping("/addRole")
    public ResponseResult addRole(@RequestBody  Role role){
        System.out.println(role);
        if(role.getRname()=="" || role.getRdescribe()==""){
            return new ResponseResult(null,400,"增加角色失败，请检查信息是否完整");
        }else{
            int count = roleServer.addRole(role);
            if (count==1){
                return new ResponseResult(count,200,"增加角色'"+role.getRname()+"'成功");
            }else {
                return new ResponseResult(count,400,"增加角色'"+role.getRname()+"'失败");
            }
        }

    }
    @PostMapping("/editRoleInfo")
    public ResponseResult editRoleInfo(@RequestBody Role role){
        System.out.println(role);
        if(role.getRname()=="" || role.getRdescribe()==""){
            return  new ResponseResult(null,500,"请输入完整信息");
        }else {
            int count = roleServer.editRoleInfo(role);
            if(count==1) {
                return new ResponseResult(count, 200, "修改成功");
            }else{
                return new ResponseResult(0,400,"修改失败");
            }

        }
    }
    @PostMapping("/deleteRole")
    public ResponseResult deleteRole(@RequestBody Role role){

        if(role.getRid()==0|| role.getRid()==null){
            return new ResponseResult(null,400,"修改失败，请检查是否正确");
        }else{
            int count = roleServer.deleteRole(role);
            if(count==1){
                return new ResponseResult(count,200,"已永久删除'"+role.getRname());
            }else{
                return new ResponseResult(count,400,"删除'"+role.getRname()+"'失败");
            }
        }
    }
}
